import pagination from '@/components/pagination.vue'
import { Request, confirm, successToast, errorToast, getDate, parseTime, checkPermission } from '@/utils'
import { chargePriceChargeTypeList, orderStatus } from '@/config'

let request = new Request()

export default {
	
	data () {
		return {
			agentList : [],
			storeList : [],
			filter : {
				type : 1,
				agentId : '',
				storeId : '',
				searchType : '',
				searchKey : '',
				status : '',
				date : []
			}, 
			
			pageQuery : {
				type : 1
			},

			chargePriceChargeTypeList : chargePriceChargeTypeList,
			orderList : [],
			
			downloadLoading : false
		}
	},
	
	components : {
		pagination
	},
	
	methods : {
		checkPermission : checkPermission,
		search : function(){
			let filter = Object.assign({}, this.filter)
			
			let date = filter.date
			
			if(date){
				filter.start = date[0]
				filter.end = date[1]
			}
			
			if(filter.searchType == 1) {
				filter.orderNo = filter.searchKey
			}else if(filter.searchType == 2) {
				filter.deviceNo = filter.searchKey
			}
			
			delete filter.date
			delete filter.searchType
			delete filter.searchKey
			
			this.pageQuery = filter
			
			this.$nextTick(() => {
				this.$refs.pagination.initPage()
			})

		},
		
		page : function(rows){
			this.deviceList.splice(0)
			rows.forEach(v => {
				this.deviceList.push(v)
			})
		},
		
		handleDownload() { //导出
			this.downloadLoading = true
			var param = Object.assign({},this.pageQuery)

			request.get("order/exportChargeOrder",res=>{
				var list = []
				this.downloadLoading = false
				if(res.code == 0){
					try{
						var payTypeList = this.payTypeList
						res.data.forEach(v=>{
							var item = [v.orderNo, payTypeList[v.payType], v.userMobile, v.deviceNo, v.devicePort, v.cardUID, (v.chargeStartTime), (v.chargeEndTime), v.consumeTime, v.consumeEnergy, v.prepaidAmount, v.actuallyAmount, v.refundAmount]
							list.push(item)
						})
						import('@/vendor/Export2Excel').then(excel => {
							try{
								const tHeader = ['订单号', '支付类型', '绑定账号手机号', '设备号', '端口号', '卡号', '开始时间', '结束时间', '充电时长（分钟）', '用电量（度）', '预付金额（元）', '实际消费金额（元）', '退款金额（元）']
		
								
								
								excel.export_json_to_excel({
									header: tHeader,
									data : list,
									filename: "充电单汇总",
								})
							}catch(e){
								console.log(e.message)
							}
						}).catch(e => {
							console.log(e.message)
						})
					}catch(e){
						console.log(e.message)
					}
				}else{
					errorToast(res.msg, this)
				}
				
				
			},param)
			
		},
	},
	
	created () {
		let d = getDate()
		this.filter.date = [d, d]
		this.pageQuery.start = d
		this.pageQuery.end = d
		
		if(checkPermission('admin')){
			request.get('agent/dropSelect', res => {
				if(res.success){
					this.agentList = res.data
				}
			})
		}else if(checkPermission('store')){
			request.get('store/dropSelect', res => {
				if(res.success){
					this.storeList = res.data
				}
			})
		}
		
	}
}